package com.baoyouqun.controller.weixin;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.UserAddressCreateCmd;
import com.baoyouqun.domain.Query.UserAddressQuery;
import com.baoyouqun.domain.UpdateCmd.UserAddressUpdateCmd;
import com.baoyouqun.domain.VO.UserAddressVO;
import com.baoyouqun.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wx/userAddress")
@Api(tags = "用户收货地址表管理接口")
public class WxUserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @PostMapping("create")
    @ApiOperation("新增用户收货地址表")
    public SingleResponse<UserAddressVO> create(@Valid @RequestBody UserAddressCreateCmd cmd) {
        String userId = StpUtil.getLoginIdAsString();
        cmd.setUserId(userId);
        return userAddressService.create(cmd);
    }

    @PutMapping("update")
    @ApiOperation("修改用户收货地址表")
    public Response update(@Valid @RequestBody UserAddressUpdateCmd cmd) {
        return userAddressService.update(cmd);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据ID删除用户收货地址表")
    public Response delete(String id) {
        userAddressService.removeById(id);
        return Response.buildSuccess();
    }


    @GetMapping("page")
    @ApiOperation("分页查询用户收货地址表")
    public PageResponse<UserAddressVO> page(UserAddressQuery query) {
//        String userId = StpUtil.getSession().getString(SystemConstant.SA_TOKEN_USER_INFO);
        String userId = StpUtil.getLoginIdAsString();
        query.setUserId(userId);
        IPage<UserAddressVO> page = userAddressService.pageVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }
}